import logging
from flask_restplus import Resource, Namespace, fields, marshal_with
from common.utils.decorators import login_required
from flask import g, request, jsonify
from app.models import db
from app import redis_clent, app
from flask_restplus import reqparse, inputs
from app.tasks.test import test
from app.routes.user.schemas import UserSchemas
from .marshmallow import (
    UserRegistrationQuerySerializer,
    UserRegistrationBodySerializer,
    UserInfoQuerySerializer,
    UserBodySerializer,
    UserDataSerizlizer,
)
from ...models.user import User

LOG = logging.getLogger(__name__)

user_ns = Namespace("user")

user_mh = UserSchemas(user_ns)


class UserLoginView(Resource):
    # method_decorators = {"get": [login_required]} # 检查用户登录的装饰器
    @user_ns.expect(user_mh.user_registr_query(), validate=True)
    @user_ns.marshal_with(user_mh.user_registr_body())
    def post(self):
        user = UserRegistrationQuerySerializer().load(data=request.json)
        response = UserRegistrationBodySerializer().dump(user)
        return response


class UserInfoView(Resource):
    @user_ns.doc(params=user_mh.user_list_query())
    @user_ns.marshal_with(user_mh.user_list_body())
    def get(self):
        users, total = UserInfoQuerySerializer().load(data=request.args)
        response = UserDataSerizlizer().dump({"total": total, "data": users})
        return response
